<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/15
 * Time: 16:54
 */
class Http
{
    const HOST = '0.0.0.0';
    const PORT = 8811;
    public $http = null;
    public function __construct()
    {
        $this->http = new swoole_http_server(self::HOST, self::PORT);

        $this->http->set([
            'enable_static_handler' => true,
            'document_root' => '/data/wwwroot/swoole/thinkphp/public/static',
            'worker_num' => 4,
            'task_worker_num' => 4
        ]);

        $this->http->on('workerStart', [$this, 'onWorkerStart']);
        $this->http->on('request', [$this, 'onRequest']);
        $this->http->on('task', [$this, 'onTask']);
        $this->http->on('finish', [$this, 'onFinish']);
        $this->http->on('close', [$this, 'onClose']);

        $this->http->start();
    }

    /**
     * @todo 监听workerstart
     * @param $server
     * @param $worker_id
     */
    public function onWorkerStart($server, $worker_id)
    {
        // 定义应用目录
        define('APP_PATH', __DIR__ . '/../application/');
        // 1. 加载基础文件
        //require __DIR__ . '/../thinkphp/base.php';
        require __DIR__ . '/../thinkphp/start.php';
    }

    /**
     * @todo 监听request
     * @param $request
     * @param $reponse
     */
    public function onRequest($request, $reponse)
    {
        $_SERVER = [];
        if(isset($request->server)){
            foreach($request->server as $k=>$v){
                $_SERVER[strtoupper($k)] = $v;
            }
        }

        if(isset($request->header)){
            foreach($request->header as $k=>$v){
                $_SERVER[strtoupper($k)] = $v;
            }
        }
        $_GET = [];
        if(isset($request->get)){
            foreach($request->get as $k=>$v){
                $_GET[$k] = $v;
            }
        }
        $_POST = [];
        if(isset($request->post)){
            foreach($request->post as $k=>$v){
                $_POST[$k] = $v;
            }
        }
        $_POST['http_server'] = $this->http;

        ob_start();
        // 执行应用并响应
        try{
            think\Container::get('app', [APP_PATH])
                ->run()
                ->send();
        }catch (\Exception $e){
            //todo
        }
        $content = ob_get_contents();
        ob_end_clean();

        $reponse->end($content);
    }

    /**
     * 监听task事件
     */
    public function onTask($serv, $task_id, $work_id, $data)
    {
        //分发task任务机制，让不同任务，走不同的逻辑
        $obj = new app\common\lib\task\Task;
        $method = $data['method'];
        $flag = false;
        //var_dump(method_exists($obj , $method));
        if(method_exists($obj , $method)){
            $flag = $obj->$method($data['data']);
        }

        return $flag;
    }

    /**
     * 监听任务完成事件
     * @param $data 注意，这里的data是onTask 返回的内容
     */
    public function onFinish($serv, $task_id, $data)
    {
        echo "taskId:{$task_id}" . PHP_EOL;
        echo "finish-data-success:{$data}--" .date('Y-m-d H:i:s') . PHP_EOL;
    }

    /**
     *  监听关闭事件
     * @param $ws
     * @param $fd
     */
    public function onClose($ws, $fd)
    {
        echo "client {$fd} closed" . PHP_EOL;
    }

}

new http();